/**
 * @param {number[]} prices
 * @return {number}
 */
 var maxProfit = function(prices) {  // 只要股票降价，就立即买入，涨到不会再涨了，立即卖出
    let buy=prices[0],res=0,ressum=0;
    for(let i=1;i<prices.length;i++){
        if(prices[i]>prices[i-1]){
            res=Math.max(res,prices[i]-buy);
        }
        else {
            ressum+=res;
            res=0;
            buy=prices[i];
        }
    }
    ressum+=res;
    return ressum;
};
console.log(maxProfit([1,2,3,4,5]));